# coding:utf-8
"""
Author: r.xw
Date: 2023/12/9
File: database.py
"""

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session, declarative_base

from app.config.config import config
from app.settings import env


def db_connect():
    # 创建一个引擎，目的是连接到我们的数据库上
    engine = create_engine(config[env].db_url, echo=config[env].echo)
    # 打开数据库的连接会话
    session = sessionmaker(engine)
    # 线程安全
    db_session = scoped_session(session)
    # 基类
    base = declarative_base()

    return db_session, base, engine
